const { ccclass, property } = cc._decorator;
import WinFormBase from '../WinFormBase';
import timerControl from '../Play/timerControl';
import GameOverControl from '../Play/GameOverControl';
import skillCounter from '../skillCounter';
import MyEnums from '../MyEnums';


class ScoreAward {
    ScoreType: number = 0;
    ScoreValue: number = 0;
    AwardType: number = 0;
    AwardValue: string = "";
}
class Award {
    AwardType: number = 0;
    AwardValue: string = "";
}

@ccclass
export default class gameControl extends cc.Component {
    @property(cc.Node)
    timerNode: cc.Node = null;
    @property(cc.Node)
    GameFail: cc.Node = null;
    @property(cc.Node)
    GameOver: cc.Node = null;
    @property(cc.Node)
    GameStop: cc.Node = null;
    @property
    rebirth: number = 1;
    @property
    birthMoney: number = 10;
    pass: number[] = [];//结束标志
    ScoreAward: ScoreAward[] = [];//评分奖励标准
    FirstAward: Award[] = [];//首通奖励
    RandomAward: boolean = false;//随机奖励
    _ScoreAward: Award[] = [];//评分奖励
    _score: number = 0;//评分
    _win: boolean = false;//输赢
    _haoshi: number = 0;//游戏耗时
    _rebirthed: number = 0;//已复活次数
    onLoad() {
        let hero = HeroManager.getInstance();
        hero.playTime = this.pass[0];
        hero.passkillCounts = this.pass[1];
        hero.passHitCounts = this.pass[2];
    }
    //游戏暂停
    gameStop() {
        let formNode = new cc.Node();
        let formControl = formNode.addComponent(WinFormBase);
        formControl.pauseScene = true;
        formControl.isBgClose = false;
        formControl.mainNode = this.GameStop;
        this.node.addChild(formNode);
    }
    //游戏超时
    TimeOut(event) {
        this.getWin();
        this.gameOver();
    }
    //kda达到限制
    kdaOut(event) {
        this._win = event.iswin;
        this.gameOver();
    }
    //主角死亡
    dieOut(event) {
        this._win = false;
        if (this.rebirth > this._rebirthed) {
            this._rebirthed++;
            if (this.disManey(this.birthMoney) > 0)
                this.gameFail();
            else
                this.gameOver();
        }
        else
            this.gameOver();
    }
    //主角死亡
    gameFail() {
        let formNode = new cc.Node();
        let formControl = formNode.addComponent(WinFormBase);
        formControl.pauseScene = true;
        formControl.mainNode = this.GameFail;
        this.node.addChild(formNode);
    }
    //加载大厅
    loadLobby() {
        cc.director.loadScene("Lobby");
    }
    //游戏结束(结算)
    gameOver() {
        this.sumGame();
        this.SetGameOver();
        let formNode = new cc.Node();
        let formControl = formNode.addComponent(WinFormBase);
        var pageViewEventHandler = new cc.Component.EventHandler();
        pageViewEventHandler.target = this.node;
        pageViewEventHandler.component = "gameControl"
        pageViewEventHandler.handler = "loadLobby";
        formControl.CloseEvents.push(pageViewEventHandler);
        formControl.pauseScene = true;
        formControl.isBgClose = true;
        formControl.mainNode = this.GameOver;
        this.node.addChild(formNode);
    }
    //结算界面
    SetGameOver() {
        let isFirstWin = false;
        if (this._win) {//记录评星成绩
            let gameH = gameHelper.getInstance();
            let _levelParam = gameH.levelParam;
            if (_levelParam.length == gameH.selectLevelId && _levelParam[gameH.selectLevelId - 1] == 0) {//首次通关
                isFirstWin = true;
                _levelParam[gameH.selectLevelId - 1] = this._score;
                if (gameH.selectLevelId < gameH.levelLength)
                    _levelParam.push(0);
            }
            else if (_levelParam.length == 0) {
                isFirstWin = true;
                _levelParam.push(this._score);
                _levelParam.push(0);
            }
            else {
                if (!_levelParam[gameH.selectLevelId - 1] || _levelParam[gameH.selectLevelId - 1] < this._score)
                    _levelParam[gameH.selectLevelId - 1] = this._score;
                if (_levelParam.length <= gameH.selectLevelId)
                    _levelParam.push(0);
            }
            gameHelper.getInstance().levelParam = _levelParam;
            gameH.selectLevelId = _levelParam.length;
        }
        if (isFirstWin) {
            this._ScoreAward = this._ScoreAward.concat(this.FirstAward);
        }

        let control = this.GameOver.getComponent(GameOverControl);
        let hero = HeroManager.getInstance();
        control.jishaNode.string = hero.killCounts.toString();
        control.shoushangNode.string = hero.HitCounts.toString();
        control.haoshiNode.string = this._haoshi.toString();
        control._score = this._score;
        control._isWin = this._win;
        control._isFirstWin = isFirstWin;
        control._ScoreAward = this._ScoreAward;
        control._RandomAward = this.RandomAward;
    }
    //判断输赢
    private getWin(): boolean {
        let hero = HeroManager.getInstance();
        this._win = false;
        if (this.pass[1] > 0) {//击杀目标
            if (this.pass[1] <= hero.killCounts)
                this._win = true;
        }
        else if (this.pass[2] > 0) {//受伤次数
            if (this.pass[2] >= hero.HitCounts)
                this._win = true;
        }
        else if (this.pass[0] > 0) {//限时挑战
            this._win = true;
        }
        return this._win;
    }
    //结算
    private sumGame() {
        let hero = HeroManager.getInstance();
        let _skillCounter: skillCounter = new skillCounter();
        _skillCounter.setCounter(hero.killCounts);//统计击杀数

        let control = this.timerNode.getComponent(timerControl);
        this._haoshi = control.startTime - control.timeing;
        if (this._win)
            this.getScore();
    }
    //计算评分和奖励
    private getScore() {
        this._score = 0;
        let hero = HeroManager.getInstance();
        let self = this;

        this.ScoreAward.forEach(element => {
            if (element.ScoreType == MyEnums.ScoreType.hurt) {
                if (hero.HitCounts <= element.ScoreValue) {
                    self._score++;
                    self._ScoreAward.push({ AwardType: element.AwardType, AwardValue: element.AwardValue })
                }
            }
            else if (element.ScoreType == MyEnums.ScoreType.skill) {
                if (hero.killCounts >= element.ScoreValue) {
                    self._score++;
                    self._ScoreAward.push({ AwardType: element.AwardType, AwardValue: element.AwardValue })
                }
            }
            else {
                if (this._haoshi <= element.ScoreValue) {
                    self._score++;
                    self._ScoreAward.push({ AwardType: element.AwardType, AwardValue: element.AwardValue })
                }
            }
        });
    }
    //消耗金币
    private disManey(money: number): number {
        let grade = gameHelper.getInstance().gradeParam;
        if (grade.money < money)
            return -1;
        grade.money -= money;
        gameHelper.getInstance().gradeParam = grade;
        return grade.money;
    }
}
